package com.ebupt.migu.music.schedule.controller;

import com.ebupt.migu.music.schedule.service.SMSTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Map;

/**
 * Created by Intellij IDEA.
 * User:  liuyangyang@cd.ebupt.com
 * Date:  2019/10/29
 */
@Slf4j
@Component
@RestController
@EnableScheduling
@RequestMapping("/sms")
public class SMSTask {

    @Autowired
    private SMSTaskService smsTaskService;

    /**
     * 短信策略： 立即发送短信
     * 每分钟执行一次定时任务
     */
    @Scheduled(initialDelay = 1000, fixedDelay = 1000 * 60)
    public void sendMessageNow() {
        log.info("短信策略【立即发送短信】定时任务启动");
        smsTaskService.sendMessageNow();
        log.info("短信策略【立即发送短信】定时任务结束");
    }

    /**
     * 短信策略： 单次发送短信
     * 每五分钟执行一次定时任务
     */
    @Scheduled(initialDelay = 2000, fixedDelay = 1000 * 60)
    public void sendMessageOnce() {
        log.info("短信策略【单次发送短信】定时任务启动");
        smsTaskService.sendMessageOnce();
        log.info("短信策略【单次发送短信】定时任务结束");
    }

    /**
     * 短信策略： 策略周期内重复执行短信策略
     * 每五分钟执行一次定时任务
     */
    @Scheduled(initialDelay = 3000, fixedDelay = 1000 * 60 * 5)
    public void sendMessageLoop() throws ParseException {
        log.info("短信策略【循环发送短信】定时任务启动");
        smsTaskService.sendMessageLoop();
        log.info("短信策略【循环发送短信】定时任务结束");
    }

    /**
     * 短信策略： 策略周期内间隔执行短信策略
     * 每五分钟执行一次定时任务
     */
    @Scheduled(initialDelay = 5000, fixedDelay = 1000 * 60 * 5)
    public void sendMessageInterval() throws ParseException {
        log.info("短信策略【间隔发送短信】定时任务启动");
        smsTaskService.sendMessageInterval();
        log.info("短信策略【间隔发送短信】定时任务结束");
    }

    @RequestMapping("/testSendSms")
    public String testSendMegamarket(@RequestBody Map<String,Object> map){
        log.info(">>>>>>>>>>进行大营销平台短信下发测试");
        return smsTaskService.testSendMegamarket(map);
    }
}
